package com.xzbd.springbootmybatiesdm.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;

import javax.servlet.http.HttpServletResponse;

import com.xzbd.springbootmybatiesdm.config.UploadConfig;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
@RequestMapping("download")
public class DownLoadController {

	@Autowired
	private UploadConfig UploadConfig;

	@RequestMapping("pic")
	public void downPic(HttpServletResponse response) throws Exception {
		log.info("download pic is runing");
       // 文件地址，真实环境是存放在数据库中的
		File file = new File(UploadConfig.getPicPath() + "/ece4b8dd-24c8-42c6-9699-e2702a87a5dd.jpg");
		// 穿件输入对象
		FileInputStream fis = new FileInputStream(file);
		// 设置相关格式
		response.setContentType("application/force-download");
		// 设置下载后的文件名以及header
		response.addHeader("Content-disposition", "attachment;fileName=" + file.getName());
		// 创建输出对象
		OutputStream os = response.getOutputStream();
		// 常规操作
		byte[] buf = new byte[1024];
		int len = 0;
		while((len = fis.read(buf)) != -1) {
			os.write(buf, 0, len);
		}
		fis.close();
	}
	@RequestMapping("file")
	public void downFile(HttpServletResponse response) throws Exception {
       // 文件地址，真实环境是存放在数据库中的
		File file = new File(UploadConfig.getDocPath() + "/ed7dda70-90e6-47a7-90b7-24411fed9313.zip");
		// 穿件输入对象
		FileInputStream fis = new FileInputStream(file);
		// 设置相关格式
		response.setContentType("application/force-download");
		// 设置下载后的文件名以及header
		response.addHeader("Content-disposition", "attachment;fileName=" + file.getName());
		// 创建输出对象
		OutputStream os = response.getOutputStream();
		// 常规操作
		byte[] buf = new byte[1024];
		int len = 0;
		while((len = fis.read(buf)) != -1) {
			os.write(buf, 0, len);
		}
		fis.close();
    }
}